package com.tongji.housediy.view.titlewindows
{
	import com.adobe.images.PNGEncoder;
	import com.tongji.housediy.events.designEvent.SaveDeleteDesignEvent;
	import com.tongji.housediy.model.ExchangeDataModel;
	import com.tongji.housediy.model.UserInforModel;
	import com.tongji.housediy.view.components.AbstractDrawingRoom;
	import com.tongji.housediy.view.components.IndependentFloor;
	import com.tongji.housediy.vo.Design;
	import com.tongji.housediy.vo.House;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.net.NetConnection;
	import flash.net.Responder;
	import flash.net.URLLoader;
	import flash.utils.ByteArray;
	
	import mx.controls.Alert;
	import mx.core.UIComponent;
	import mx.events.CloseEvent;
	import mx.graphics.codec.JPEGEncoder;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/4/18
	 */ 
	public class SaveDesignWinMediator extends Mediator
	{
		[Inject]
		public var saveDesignView:SaveDesignTitleWindow;
		
		[Inject]
		public var roomModel:ExchangeDataModel;
		
		[Inject]
		public var userModel:UserInforModel;
		
		private var nc:NetConnection;
		override public function onRegister():void
		{
			saveDesignView.closeBtn.addEventListener(MouseEvent.CLICK, closeWindowHandler);
			saveDesignView.cancleBtn.addEventListener(MouseEvent.CLICK, closeWindowHandler);
			saveDesignView.roomImg.source = new Bitmap(getImg());
			saveDesignView.saveDesignBtn.addEventListener(MouseEvent.CLICK, saveDesignHandle);
		}
		
		private function closeWindowHandler(event:MouseEvent):void
		{
			PopUpManager.removePopUp(saveDesignView);
			mediatorMap.removeMediatorByView(saveDesignView);
		}
		
		private function getImg():BitmapData
		{
			var minX:Number = 10000;
			var minY:Number = 10000;
			var maxX:Number = -1;
			var maxY:Number = -1;
			var target:UIComponent = roomModel.gridContainer;
			var boundingBox:Rectangle;
			for (var i:int = 0; i < roomModel.gridContainer.numChildren; i++)
			{
				if ((roomModel.gridContainer.getChildAt(i) is AbstractDrawingRoom) && !(roomModel.gridContainer.getChildAt(i) is IndependentFloor))
				{
					var host:AbstractDrawingRoom = roomModel.gridContainer.getChildAt(i) as AbstractDrawingRoom;
					boundingBox = host.getRect(host.parent);
					if (boundingBox.x <= minX)
						minX = boundingBox.x;
					if (boundingBox.y <= minY)
						minY = boundingBox.y;
					if (boundingBox.x + boundingBox.width >= maxX)
						maxX = boundingBox.x + boundingBox.width;
					if (boundingBox.y + boundingBox.height >= maxY)
						maxY = boundingBox.y + boundingBox.height;
				}					
			}
			var matrix:Matrix = new Matrix();
			matrix.translate(-minX + 32, -minY + 32);
			var bitmapData:BitmapData = new BitmapData(maxX - minX + 64, maxY - minY + 64);
			bitmapData.draw(target,matrix); 
			roomModel.roomImg = bitmapData;
			return bitmapData;
		}
		
		private function saveDesignHandle(event:MouseEvent):void
		{
			/*var design:Design = new Design();*/
			if(userModel.userID == '')
			{
				Alert.show("请先登录");
			}
			else
			{
			/*	design.userID = int(userModel.userID);*/
				var encode:JPEGEncoder = new JPEGEncoder(100);
				var byte:ByteArray = encode.encode(getImg());
				
				nc = new NetConnection();
				var rp:Responder = new Responder(successResult, errStatus);
				nc.client = nc;
				
				nc.objectEncoding = flash.net.ObjectEncoding.AMF3;
				//nc.connect("http://10.60.0.118/housediy/Gateway.aspx");
				nc.connect("http://design.rryjw.com/Gateway.aspx");
				var date:Date = new Date();
				var fileName:String =  userModel.userID.toString() + date.fullYear.toString() + date.month.toString() + 
					date.date.toString() + date.hours.toString() + date.minutes.toString() + date.seconds.toString();
				nc.call("HouseDiyService.HouseDiyAction.getPic",rp, byte, fileName);
				var loader:URLLoader = new URLLoader;
				
				/*design.designCreateDate = new Date();
				design.designName = saveDesignView.designNameTX.text;
				design.designPicSrc = fileName + ".jpg";
				design.isReleased = false;
				design.xmlSrc = roomModel.saveHouse().toString();
				
				dispatch(new SaveDeleteDesignEvent(SaveDeleteDesignEvent.SAVE_DESIGN, design));
				PopUpManager.removePopUp(saveDesignView);
				mediatorMap.removeMediatorByView(saveDesignView);*/
			}
			
		}
		
		private function successResult(result:String):void 
		{
			//关闭连接
			var design:Design = new Design();
			design.userID = int(userModel.userID);
			design.designCreateDate = new Date();
			design.designName = saveDesignView.designNameTX.text;
			design.designPicSrc = result;
			design.isReleased = false;
			design.xmlSrc = roomModel.saveHouse().toString();
			
			dispatch(new SaveDeleteDesignEvent(SaveDeleteDesignEvent.SAVE_DESIGN, design));
			PopUpManager.removePopUp(saveDesignView);
			mediatorMap.removeMediatorByView(saveDesignView);
			nc.close();
		}
		
		
		private function errStatus(result:String):void 
		{
			nc.close();
		}
	}
}